#!/bin/bash

# 自动 Git 提交脚本
# 默认分支: master

echo "=== 自动 Git 提交 ==="

if [ ! -d ".git" ]; then
    echo "错误: 当前目录不是 Git 仓库"
    exit 1
fi

# 检查是否有更改
if [ -z "$(git status --porcelain)" ]; then
    echo "没有需要提交的更改"
    exit 0
fi

# 显示更改状态
echo "当前更改:"
git status -s

# 添加所有更改
echo "添加所有更改到暂存区..."
git add .

# 提交信息
if [ -z "$1" ]; then
    COMMIT_MSG="更新: $(date '+%Y-%m-%d %H:%M:%S')"
else
    COMMIT_MSG="$1"
fi

echo "提交信息: $COMMIT_MSG"
git commit -m "$COMMIT_MSG"

# 推送到远程仓库
echo "推送到远程仓库..."
CURRENT_BRANCH=$(git branch --show-current)

if [ "$CURRENT_BRANCH" = "main" ]; then
    git push origin main
else
    git push origin master
fi

echo "=== 提交完成 ==="

